package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        //新建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //判断购物车数据库中是否已经存储
        List<ShoppingCart> list = shoppingCartMapper.getBy(shoppingCart);
        //如果以存储则数量加一，若未存储，则进行新增
        if(list != null && list.size() > 0){
            ShoppingCart cartGet = list.get(0);
            cartGet.setNumber(cartGet.getNumber()+1);
            shoppingCartMapper.update(cartGet);
        }else {
            //补全数据
            //如果套餐id为空则加入的是菜品，则查询菜品数据
            if(shoppingCart.getSetmealId() == null){
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }else {
                Setmeal setmeal = setMealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            //购物车新增
            shoppingCartMapper.add(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> getByUser() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.getBy(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        shoppingCartMapper.delete(shoppingCart);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param dto
     */
    @Override
    public void sub(ShoppingCartDTO dto) {
        //新建对象
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(dto,shoppingCart);
        //获取对象
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getBy(shoppingCart);
        if(shoppingCarts != null && shoppingCarts.size()>0){
            shoppingCart = shoppingCarts.get(0);
            if(shoppingCart.getNumber()==1){
                shoppingCartMapper.delete(shoppingCart);
            }else {
                shoppingCart.setNumber(shoppingCart.getNumber() -1);
                shoppingCartMapper.update(shoppingCart);
            }
        }
    }
}
